import koa from "koa";
import bodyParser from "koa-bodyparser";
import dotenv from "dotenv";

import { crossDomain } from "./utils/cross-domain";
import { registerRouter } from "./router";
import { chalkERROR, chalkSUCCESS, chalkWARN, emoji } from "./utils/chalkTip";

const app = new koa();

// 加载 env 文件
dotenv.config();

app.use(crossDomain);
app.use(bodyParser());

// 注册路由
registerRouter(app);

new Promise((resolve, reject) => {
    app.listen(process.env.SERVER_PORT, () => {
        resolve("ok");
    });
})
    .then(() => {
        // 启动成功后提示信息：
        console.log();
        console.log(chalkSUCCESS(`http服务启动成功！`));
        console.log(chalkWARN(`监听端口: ${process.env.SERVER_PORT}`));
        // console.log(chalkWARN(`项目名称: ${PROJECT_NAME}`));
        // console.log(chalkWARN(`项目环境: ${PROJECT_ENV}`));
        // console.log(chalkWARN(`mysql数据库: ${MYSQL_CONFIG.database}`));
    })
    .catch(err => {
        console.log(chalkERROR("项目启动失败！"));
        console.log(err);
    });
